package inputPackage;

import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Properties;

public class KeyResource {

	private ArrayList<Integer> defaultKeys;
	private ArrayList<Integer> newKeys;
		
	static private int right2D = 0;
	static private int left2D = 1;
	static private int right3D = 2;
	static private int left3D = 3;
	static private int up3D = 4;
	static private int down3D = 5;
	static private int jump = 6;
	static private int switchViews = 7;
	static private int pause = 8;
	
	private Properties keyValue;
	
	public KeyResource() {
		
		defaultKeys = new ArrayList<Integer>();
		newKeys = new ArrayList<Integer>();
		
		
		
		keyValue = new Properties();
		try {
			keyValue.load(this.getClass().getClassLoader().getResourceAsStream("keyConfig.properties"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
				
		defaultKeys.add(KeyEvent.VK_RIGHT);
		defaultKeys.add(KeyEvent.VK_LEFT);
		defaultKeys.add(KeyEvent.VK_RIGHT);
		defaultKeys.add(KeyEvent.VK_LEFT);
		defaultKeys.add(KeyEvent.VK_UP);
		defaultKeys.add(KeyEvent.VK_DOWN);
		defaultKeys.add(KeyEvent.VK_SPACE);
		defaultKeys.add(KeyEvent.VK_SHIFT);
		defaultKeys.add(KeyEvent.VK_ESCAPE);
		
		newKeys.add(0);
		newKeys.add(0);
		newKeys.add(0);
		newKeys.add(0);
		newKeys.add(0);
		newKeys.add(0);
		newKeys.add(0);
		newKeys.add(0);
		newKeys.add(0);
		
	}	
	
	
	/*
	 * Setters for the new keys.
	 * */
	public void setRight2D(int k) {
		newKeys.set(right2D, k);
	}
	public void setLeft2D(int k) {
		newKeys.set(left2D, k);
	}
	public void setRight3D(int k) {
		newKeys.set(right3D, k);
	}
	public void setLeft3D(int k) {
		newKeys.set(left3D, k);
	}
	public void setUp3D(int k) {
		newKeys.set(right2D, k);
	}
	public void setDown3D(int k) {
		newKeys.set(left2D, k);
	}
	public void setJump(int k) {
		newKeys.set(jump, k);
	}
	public void setSwitchViews(int k) {
		newKeys.set(switchViews, k);
	}
	public void setPause(int k) {
		newKeys.set(pause, k);
	}
	
	
	/*
	 * Getters for the new keys.
	 * */
	public int getRight2D() {
		return newKeys.get(right2D);
	}
	public int getLeft2D() {
		return newKeys.get(left2D);
	}
	public int getRight3D() {
		return newKeys.get(right3D);
	}
	public int getLeft3D() {
		return newKeys.get(left3D);
	}
	public int getUp3D() {
		return newKeys.get(up3D);
	}
	public int getDown3D() {
		return newKeys.get(down3D);
	}
	public int getJump() {
		return newKeys.get(jump);
	}
	public int getSwitchViews() {
		return newKeys.get(switchViews);
	}
	public int getPause() {
		return newKeys.get(pause);
	}
	
	
	/*
	 * Saves the new keys to a file.  Do this on exiting the key config menu, or starting the game.
	 * 
	 * THIS DOESNT WORK RIGHT NOW???  WHY??? (It still works in game, but you have to set up your config each 
	 * you start the game.  :C )
	 * 
	 * */
	@SuppressWarnings("deprecation")
	public void writeToFile() {
			
		
		keyValue.setProperty("right2D", Integer.toString(newKeys.get(right2D)));
		keyValue.setProperty("left2D", Integer.toString(newKeys.get(left2D)));
		keyValue.setProperty("right3D", Integer.toString(newKeys.get(right3D)));
		keyValue.setProperty("left3D", Integer.toString(newKeys.get(left3D)));
		keyValue.setProperty("up3D", Integer.toString(newKeys.get(up3D)));
		keyValue.setProperty("down3D", Integer.toString(newKeys.get(down3D)));
		keyValue.setProperty("jump", Integer.toString(newKeys.get(jump)));
		keyValue.setProperty("switchViews", Integer.toString(newKeys.get(switchViews)));
		keyValue.setProperty("pause", Integer.toString(newKeys.get(pause)));
		
			
		File f = new File("keyConfig.properties");
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(f);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		keyValue.save(fos, "");
		
		
	}
	
	
	
	
	/*
	 * Read key config from file.  Do this on game start.
	 * */
	public void readFromFile() {
		
		newKeys.set(right2D, Integer.parseInt(keyValue.getProperty("right2D")));
		newKeys.set(left2D, Integer.parseInt(keyValue.getProperty("left2D")));
		newKeys.set(right3D, Integer.parseInt(keyValue.getProperty("right3D")));
		newKeys.set(left3D, Integer.parseInt(keyValue.getProperty("left3D")));
		newKeys.set(up3D, Integer.parseInt(keyValue.getProperty("up3D")));
		newKeys.set(down3D, Integer.parseInt(keyValue.getProperty("down3D")));
		newKeys.set(jump, Integer.parseInt(keyValue.getProperty("jump")));
		newKeys.set(switchViews, Integer.parseInt(keyValue.getProperty("switchViews")));
		newKeys.set(pause, Integer.parseInt(keyValue.getProperty("pause")));
		
	}
	
	
	public void restoreDefaults() {
		for (int i = 0; i < defaultKeys.size(); i++) {
			newKeys.set(i, defaultKeys.get(i));
		}
	}
}
